﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;

namespace TMDT.Web.UI.Controls
{
    public partial class NhaSanXuatControl : System.Web.UI.UserControl
    {
        private static Guid NhaSanXuat { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                LoadRepeater();
        }

        #region Methods

        private void LoadRepeater()
        {
            mtvView.ActiveViewIndex = 0;

            rptNhaSanXuat.DataSource = new NhaSanXuatManager().GetNhaSanXuat();
            rptNhaSanXuat.DataBind();
        }

        private void LoadUpdate()
        {
            var entity = new NhaSanXuatManager().GetNhaSanXuat(NhaSanXuat);

            txtTenNhaSanXuatUpdate.Text = entity.TenNhaSanXuat;

        }

        #endregion


        #region Events

        protected void lbnInsert_Click(object sender, EventArgs e)
        {
            var entity = new NhaSanXuat
            {
                IDNhaSanXuat = Guid.NewGuid(),
                TenNhaSanXuat = txtTenNhaSanXuatInsert.Text
            };
            var service = new NhaSanXuatManager();
            service.Add(entity);
            LoadRepeater();
        }

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            var service = new NhaSanXuatManager();
            var entity = service.GetNhaSanXuat(NhaSanXuat);
            entity.TenNhaSanXuat = txtTenNhaSanXuatUpdate.Text;

            service.Update(entity);
            LoadRepeater();
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            mtvView.ActiveViewIndex = 0;
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = (((ImageButton)sender).CommandArgument);
            var service = new NhaSanXuatManager();
            service.Remove(id);
            LoadRepeater();
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new NhaSanXuatManager();
            NhaSanXuat = service.GetNhaSanXuat(id).IDNhaSanXuat;

            LoadUpdate();

            mtvView.ActiveViewIndex = 1;
        }

        #endregion
    }
}